Page({
    data: {
        doc : {} ,
        use : [] ,
        quiz : 0 ,
        task : 0 ,
        rec : 0 ,
        gro : 0 ,
        email : 0 ,
        channel:[]
    },

    onLoad : function () {
        let that =this;
        // 获取渠道列表
        // that.xGetChannel()
    },

    onShow : function () {
        this.xInit()
        this.xAcc()
        /* 点评数 */
        // this.xTask()
        /* 访谈数 */
        this.xRec()
        /* 未读数 */
        this.xQuiz()
        // 团体管理
        // this.xGetEmail()
    },

    xInit : function () {
        let that = this
        wx.cloud.callFunction({
            name: 'user',
            data: {
                action: "detail"
            }
        }).then(res => {
            console.log(res)
            let data = res.result
            that.setData({
                doc : data
            })
        })
    },

    xAcc : function () {
        let that = this
        wx.cloud.callFunction({
            name: 'digital_therapy',
            data: {
                action: "get_user_by_treatment"
            }
        }).then(res => {
            console.log("返回数据:", res)
            let data = res.result
            that.setData({
                use : data
            })
        })
    },

    xTask : function () {
        let that = this
        wx.cloud.callFunction({
            name: 'digital_therapy',
            data: {
                action: "get_toBeCommented_task_count"
            }
        }).then(res => {
            let data = res.result
            that.setData({
                task : data
            })
        })
    },

    xRec : function () {
        let that = this
        let _dataObj = {
            is_complete:false
        }
        wx.cloud.callFunction({
            name: 'digital_therapy',
            data: {
                action: "get_interview_count",
                dataObj: _dataObj
            }
        }).then(res => {
            let data = res.result
            that.setData({
                rec : data
            })
        })
    },
    // 获取渠道列表
    xGetChannel:function(){
        let that =this;
        wx.cloud.callFunction({
          name: 'channel_management',
          data: {
            action: "get_channel_list"
          }
        }).then(res => {
          that.setData({
            channel:res.result
          })
        })
    },
    // 获取小程序码
    get_wxacode:function(e){
        let that =this;
        wx.cloud.callFunction({
          name: 'channel_management',
          data: {
            action: "get_wxacode",
            channel_id: e.currentTarget.dataset.id
          }
        }).then(res => {
          const arrayBuffer = new Uint8Array(res.result.buffer)
          var url = 'data:image/jpeg;base64,' + wx.arrayBufferToBase64(arrayBuffer)
          wx.previewImage({
            urls: [url],
          })
        })

    },
    // 团体管理
    xToGro : function () {
        wx.navigateTo({
          url: '/subdter/pages/mine/gro/index/index',
        })
    },
    xToTask : function () {
        wx.navigateTo({
            url: '/subdter/pages/mine/xtask/index/index',
        })
    },

    xToRec : function () {
        wx.navigateTo({
            url: '/subdter/pages/mine/rec/index/index',
        })
    },

    xQuiz : function () {
        let that = this
        wx.cloud.callFunction({
            name: 'consulting_message',
            data: {
                action: "get_unread_messages_num"
            }
        }).then(res => {
            let data = res.result
            that.setData({
                quiz : data
            })
        })
    },

    xToQuiz : function () {
        wx.navigateTo({
            url: '/subdter/pages/mine/quiz/index/index',
        })
    },

    xToDet : function (e) {
        let params = e.currentTarget.dataset
        wx.navigateTo({
            url: '/subdter/pages/mine/xtask/detail/index',
            success: (res) => {
                res.eventChannel.emit('xToDetail', params)
            }
        })
    },

    xEdit : function (e) {
        let params = e.currentTarget.dataset
        let id = params.id
        let name = params.name
        let ava = params.ava
        let more = params.more
        wx.navigateTo({
            url: '/subdter/pages/mine/edit/index?id=' + id + '&name=' + name + '&ava=' + ava + '&more=' + more,
        })
    },

    xToEmail : function () {
        let that = this
        let doc = that.data.doc
        wx.navigateTo({
            url: '/subdter/pages/email/index/index?id=' + doc.user_id,
        })
    },

    xGetEmail: function () {
        let that = this
        wx.cloud.callFunction({
            name: 'consulting_message',
            data: {
                action: "get_untreated_letter",
            }
        }).then(res => {
            let data = res.result
            that.setData({
                email : data.length
            })
        })
    }
})